<?php
class Product extends MX_Controller {
	function __construct()
	{
		parent::__construct();
		$this->load->model('product_model');
		$this->lang->load('error', $this->public_model->getLanguage());
	}
	
	//show all products
	function index()
	{
		$data['pro_cat_list'] = $this->product_model->getProductCategoriesList(array("status" => 1));
		$page = $this->input->post("page");
		if(empty($page)) $page = 1;
		$itemsPerPage = $this->input->post("item_per_page");
		if(empty($itemsPerPage)) $itemsPerPage = $this->public_model->getSystemParameterValue("itemsPerPage");
		$numberPageDisplay = $this->public_model->getSystemParameterValue("numberPageDisplay");
		
		$total = $this->input->post('total');
		
		$options = array("status" => 1,
		                 "orderby" => array("ordering" => "asc"),
		                 "limit" => $itemsPerPage,
		                 "offset" => ($page - 1) * $itemsPerPage);
		if(empty($total)){
			$total = $this->product_model->countSearchProducts($options);
		}
		
		$data['products'] = array();
		if($total > 0){
			$data['products'] = $this->product_model->searchProducts($options);	
		}
		$pPaging = $this->commonutils->paging($page, $itemsPerPage, $total, $numberPageDisplay);
		$pPaging['pag_function'] = "searchProduct";
		$pPaging['pag_item_name'] = "product(s)";
		$pPaging['itemsPerPage'] = $itemsPerPage;
		$data['pPaging'] = $pPaging;
		
		$content = $this->load->view('list_view', $data, true);
		Template::display($content);
	}
	
	function search(){
		$total = $this->input->post('total');
		$page = $this->input->post("page");
		$itemsPerPage = $this->input->post("item_per_page");
		if(empty($itemsPerPage)) $itemsPerPage = $this->public_model->getSystemParameterValue("itemsPerPage");
		$numberPageDisplay = $this->public_model->getSystemParameterValue("numberPageDisplay");
		
		if(empty($page)) $page = 1;
		
		$options = array("status" => 1,
						 "search_product_name" => $this->input->post("search_product_name"),
						 "category_id" => $this->input->post("category_id"),
		                 "orderby" => array("ordering" => "asc"),
		                 "limit" => $itemsPerPage,
		                 "offset" => ($page - 1) * $itemsPerPage
		                 );
		                 		
		if(empty($total)){
			$total = $this->product_model->countSearchProducts($options);
		}
		
		$pPaging = $this->commonutils->paging($page, $itemsPerPage, $total, $numberPageDisplay);
		$pPaging['pag_function'] = "searchProduct";
		$pPaging['pag_item_name'] = "product(s)";
		$pPaging['itemsPerPage'] = $itemsPerPage;
		$data['pPaging'] = $pPaging;
		$data['products'] = array();
		$html = "";
		if($total > 0){
			$data['products'] = $this->product_model->searchProducts($options);
			$html = $this->load->view('searchproduct_view', $data, true);
		}else{
			$html = "No product found";
		}
		echo $html;
	}
	
	
	function category($category_id){																
		$data['pro_cat_list'] = $this->product_model->getProductCategoriesList(array("status" => 1));
		$page = $this->input->post("page");
		if(empty($page)) $page = 1;
		$itemsPerPage = $this->input->post("item_per_page");
		if(empty($itemsPerPage)) $itemsPerPage = $this->public_model->getSystemParameterValue("itemsPerPage");
		$numberPageDisplay = $this->public_model->getSystemParameterValue("numberPageDisplay");
		
		$total = $this->input->post('total');
		
		$options = array("status" => 1,
		                 "orderby" => array("ordering" => "asc"),
		                 "category_id" => $category_id,
		                 "limit" => $itemsPerPage,
		                 "offset" => ($page - 1) * $itemsPerPage
		                 );
		if(empty($total)){
			$total = $this->product_model->countSearchProducts($options);
		}
		
		
		$data['products'] = array();
		if($total > 0){
			$data['products'] = $this->product_model->searchProducts($options);	
		}
		$pPaging = $this->commonutils->paging($page, $itemsPerPage, $total, $numberPageDisplay);
		$pPaging['pag_function'] = "searchProduct";
		$pPaging['pag_item_name'] = "product(s)";
		$pPaging['itemsPerPage'] = $itemsPerPage;
		$data['pPaging'] = $pPaging;
		$data['search_category_id'] = $category_id;
		
		$content = $this->load->view('list_view', $data, true);
		Template::display($content);
	}
	
	function detail(){
		$content = $this->load->view("detail_view", false, true);
		Template::display($content);
	}
	
	/*
	 * Hot product
	 *  - status = 1
	 *  - is_hot = 1
	 *  - limit 20
	 */
	function showHotProducts(){		
		$data = array();
		$itemsPerPage = 20;
		$total = $this->input->post('total');
		
		$options = array("is_hot" => 1,
		                 "status" => 1,
		                 "orderby" => array("ordering" => "asc"),
		                 "limit" => $itemsPerPage
		                 //"offset" => ($page - 1) * $itemsPerPage
		                 );
		if(empty($total)){
			$total = $this->product_model->countSearchProducts($options);
		}
		$data['hotproducts'] = array();
		if($total > 0){
			$data['hotproducts'] = $this->product_model->searchProducts($options);	
		}		
		
		$this->load->view('hotproducts_view', $data);
	}
	
}